<?php

/**
 * @author	barbarosalcin
 * @desc	class core
 * @package	core
 * @version	$Id: 20120113
 */

require ('smarty/Smarty.class.php');

class core extends Smarty
{
	
	public $f;
	public $db;
	public $page;
	public $session;
	public $buffer;
	public $config;
	public $postman;
	public $eh;
	public $cache = array();
	protected $_base;
	protected $_theme;
	protected $_location;
	
	/**
	 * @author barbarosalcin
	 * @desc smarty setup with predefined variables
	 */
	
	function _setupsmarty()
	{
		
		$this->setTemplateDir(array(
				'templates' => 'templates'))->setCompileDir('cache')->setCacheDir('cache');
		
		$this->force_compile = true;
		$this->caching = false;
		$this->compile_check = true;
		$this->cache_lifetime = 20;
	}
	
	/**
	 * @author barbarosalcin
	 * @desc constructor for core
	 */
	
	function __construct($config)
	{
		parent::__construct();
		$this->_setupsmarty();
		
		$this->db = new sql_db($config['db']['server'], $config['db']['user'], $config['db']['pass'], $config['db']['db']);
		
		unset($config['db']);
		
		$sql = "SELECT SQL_CACHE config_name,config_value,config_group from " . CONFIG_TABLE;
		
		$result = $this->db->sql_query($sql);
		
		while($row = $this->db->sql_fetchrow($result))
		{
			$config[$row['config_name']] = $row['config_value'];
			
			if($row['config_group'] == 'info')
			{
				$config['info'][$row['config_name']] = $row['config_value'];
			}
		}

		if (! is_dir($this->getTemplateDir('templates') . DS . $config['template_name']))
		{
			$config['template_name'] = 'default';
		}
		
		$this->config = $config;
		
		$this->f = new functions();
		
		$this->page = new page();
		
		$this->session = new session($this->db, $this->config);
		
		$this->buffer = new stdClass();
		
		$this->eh = new errorhandler();
	
	}
	
	function __toString()
	{
		
		$this->_display();
	}
	
	/**
	 * @author barbarosalcin
	 * @desc display function for core
	 */
	
	function _display()
	{
		switch ($this->page->request)
		{
			case 'json' :
				$this->page->contentType = 'application/json';
			break;
			default :
				$this->page->contentType = 'text/html';
			break;
		}
		
		$cache = 'cached_'.implode('_',$this->cache);

		$output = $this->fetch($this->getBase() .  '/body.tpl',$cache,null,null);
		
		
		if($this->page->process['info'] == true){
			$output = $this->f->process_labels($output,$this->config['info']);
		}
		
		echo $output;
	}
	
	function _error()
	{
		
		switch ($this->page->request)
		{
			case 'json' :
				$this->page->contentType = 'application/json';
				$this->page->_pv['error'] = json_encode($this->eh->_result);
			break;
			default :
				$this->page->contentType = 'text/html';
				$this->page->_pv['error'] = $this->eh->_result;
			break;
		}
		
		$this->assign('pv', $this->page->_pv);
		
		return $this->display($this->getBase() . DS . 'error.tpl');
	}
	
	/**
	 * @author barbarosalcin
	 * @desc initialization of boxes
	 * @return unknown_type
	 */
	
	function init_modules()
	{
		global $lang, $config, $req;
		$sql = "SELECT SQL_CACHE pagemodules from " . MODULES_TABLE . " where pagename = '{$this->page->_pn}'";
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$tmp = (array) json_decode('{' . $row['pagemodules'] . '}');
		
		$this->page->_pm = array_merge($this->page->_pm, $tmp);
		
		$advert = 0;
		foreach ( $this->page->_pm as $key => $value )
		{
			$file = __PHPROOT__ . 'pages' . DS . 'modules' . DS . 'module_' . $value . '.php';
			
			if (is_file($file))
			{
				$this->page->_pv[$value] = $this->compileModule($file);
				$this->page->_pb['modules'][$key] = $value . '_module';
			}
			
			if (intval($value))
			{
				$advert = 1;
			}
		}
		
		if ($advert)
		{
			$this->page->_pb['adverts'] = $this->compileModule(__PHPROOT__ . 'pages' . DS . 'modules' . DS . 'module_adverts.php');
		}
	}
	
	function compileModule($module)
	{
		return include_once ($module);
	}
	
	/**
	 * @return the $_base
	 */
	protected function getBase()
	{
		return $this->_base;
	}
	
	/**
	 * @return the $_theme
	 */
	protected function getTheme()
	{
		return $this->_theme;
	}
	
	/**
	 * @return the $_location
	 */
	protected function getLocation()
	{
		return $this->_location;
	}
	
	/**
	 * @param field_type $_base
	 */
	protected function setBase($_base)
	{
		$this->_base = $_base;
		return $this;
	}
	
	/**
	 * @param field_type $_theme
	 */
	protected function setTheme($_theme)
	{
		$this->_theme = $_theme;
		return $this;
	}
	
	/**
	 * @param field_type $_location
	 */
	protected function setLocation($_location)
	{
		$this->_location = $_location;
		return $this;
	}

}

class myexception extends Exception
{
	public function errormessage()
	{
		$errormsg = '<strong>' . $this->getmessage() . "</strong><br />\n";
		return $errormsg;
	}
}
